package com.miao.teacher.ui;

import org.json.JSONException;
import org.json.JSONObject;

import com.espeaker.sdk.Constants;
import com.espeaker.sdk.api.ReviseApi;
import com.espeaker.sdk.utils.JsonUtils;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.miao.teacher.adapter.OrderAdapter;
import com.miao.teacher.app.TApp;
import com.miao.teacher.controls.LocalRevise;
import com.miao.teacher.models.PullOrder;
import com.miao.teacher.models.ReviseInfo;
import com.espeaker.sdk.model.ReviseJson;
import com.miao.teacher.utils.DBUtil;
import com.miao.teacher.utils.FileUtil;

import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class UnfinishedFragment  extends BaseFragment{
	
	private ReviseApi api = new ReviseApi();
	private PullToRefreshListView lv = null;

	private OrderAdapter mAdapter = null;
	
	public static UnfinishedFragment newInstance() {
		UnfinishedFragment fragment = new UnfinishedFragment();
		return fragment;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.layout_revise, null);
		lv = (PullToRefreshListView) view.findViewById(R.id.lvPull);
		lv.setOnRefreshListener(new OnRefreshListener<ListView>() {
			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				// TODO Auto-generated method stub
				doReload();
			}
		});
		return view;
	}
	@Override
	protected void RecMsgFromHandler(Message msg) {
		// TODO Auto-generated method stub
		super.RecMsgFromHandler(msg);
		switch (msg.what) {
		case Constants.REVISE_UNFINISHED:
			showOrder(msg.obj);
			break;
		}
	}

	private void showOrder(Object obj) {
		try {
			final JSONObject json = new JSONObject(obj.toString());
			int status = JsonUtils.getInt(json, "status", -1);
			if (status == 0) {
				JSONObject valueJson = JsonUtils.getJSONObject(json, "values",
						null);
				PullOrder[] orders = doLoadFromNet(valueJson.toString());
				mAdapter = new OrderAdapter(getActivity().getBaseContext(),
						orders);
				lv.setAdapter(mAdapter);
				setListener();
			}
			lv.onRefreshComplete();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void doReload()
	{
		String userId = FileUtil.getPreferences(getActivity(),
				TApp.USER_ID);
		api.getUnfinished(userId, mHandler);
	}
	private void setListener() {
		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				PullOrder mCurOrder = (PullOrder) arg0.getItemAtPosition(arg2);
				if(LocalRevise.getByXiaoFeiId(new DBUtil(getActivity()).sld, mCurOrder.id).id==0)
				{
					ReviseInfo mInfo=new ReviseInfo();
					mInfo.xiaofei_id=mCurOrder.id;
					ReviseJson mJson=new ReviseJson();
					mJson.audio_length=mCurOrder.audio_length;
					mJson.audio_url=mCurOrder.audio_url;
					mJson.title=mCurOrder.title;
					mJson.xiaofei_id=mCurOrder.id;
					mJson.created_at=mCurOrder.created_at;
					String userId = FileUtil.getPreferences(getActivity(),
							TApp.USER_ID);
					mJson.revise_id=Integer.parseInt(userId);
					Gson gson=new Gson();
					mInfo.json=gson.toJson(mJson);
					LocalRevise.doAddRevise(new DBUtil(getActivity()).sld, mInfo);
				}
				ReviseActivity.createInstance(getActivity(), mCurOrder.id);
			}
		});
	}
	public PullOrder[] doLoadFromNet(String valueJson) {
		PullOrder[] jsonBean = null;
		try {
			valueJson = valueJson.replace("{\"xiaofeis\":", "");
			valueJson = valueJson.substring(0, valueJson.length() - 1);
			Gson gson = new Gson();
			jsonBean = gson.fromJson(valueJson, PullOrder[].class);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonBean;
	}@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		doReload();
	}
}
